import util from '../../common/js/util'
import NProgress from 'nprogress'
import axios from 'axios';
import address from "../../../models/dict/address.js"  //任务字典模块
import moment from 'moment'

import task from "../../../models/dict/task.js"  //任务字典模块
import array from "../../../models/comm/array.js"  //数组操作模块
import yz from "../../../models/comm/yz.js"  //数组操作模块
import Vue from 'vue'

import taskInfo from './taskInfo.vue'
import py from '../../../models/comm/webPy';
export default {
    data() {
        return {
            taskList: [],
            taskClassArray: task.classArray(),
            currObj: task.fmtTask(),

            ShowSave: false,
            page: 1,
            filters: {},

            listLoading: false,
            total: 1,

        }
    },
    methods: {


        getTaskList: function () {


            axios.post("/a", {
                class: "task",
                para: "getTaskList",
                j: {
                    data: {

                        companyCode: localStorage.companyCode

                    },
                    page: this.page


                }

            }).then((res) => {

                this.taskList = res.data.list;
                this.listLoading = false;
                NProgress.done();


            });
        },
        toSaveTask(index, _currObj) {


            this.ShowSave = true;


            Vue.set(this.currObj, task.fmtTask(this.currObj))

            if (_currObj) {

                axios.post("/a", {
                    class: "task",
                    para: "getTask",
                    j: {
                        _id: _currObj._id

                    }

                }).then((res) => {


                    this.listLoading = false;
                    this.currObj = task.fmtTask(res.data.info);
                    NProgress.done();


                });


            }
            else {


            }
        },
        fmtDate: function (row, col) {

            return moment(row.createTime).format("YYYY-MM-DD HH:mm:ss");

        },
        handleCurrentChange: function (params) {

        },
        saveTask: function (params) {
            this.currObj.companyCode = localStorage.companyCode;
            axios.post("/a", {
                class: "task",
                para: "saveTask",
                j: this.currObj

            }).then((res) => {
                if (res.data.re == "ok") {
                    this.ShowSave = false;
                    this.getTaskList();
                } else {
                    alert(JSON.stringify(res.data));
                }
            });
        },
        querySearchMember: function (query, cb) {
            var data = {
                "$or": [
                    { name: { $regex: query } },
                    { namePy: { $regex: py.firstLetter(query) } }
                ]
            }
            //  console.log(JSON.stringify(data));
            if (query !== '') {
                this.loading = true;
                axios.post("/a", {
                    class: "member",
                    para: "getMemberList",
                    j: {
                        data: data

                    }

                }).then((res) => {

                    this.loading = false;

                    var list = res.data.list;

                    list.forEach(function (member) {
                        member.value = member.name;

                    }, this);
                    cb(list)

                });

            }
        },
        selManager: function (params) {


            var b = false;
            this.currObj.managers.forEach(function (manager) {
                if (params._id == manager._id) {

                    b = true;
                    return false;
                }

            }, this);
            if (!b) {
                this.currObj.managers.push({

                    name: params.name,
                    _id: params._id
                });

            }

            //     console.log(JSON.stringify(this.currObj.managers));

        },
        selMember: function (params) {


            var b = false;
            this.currObj.members.forEach(function (member) {
                if (params._id == member._id) {

                    b = true;
                    return false;
                }

            }, this);
            if (!b) {
                this.currObj.members.push({

                    name: params.name,
                    _id: params._id
                });

            }

            //     console.log(JSON.stringify(this.currObj.managers));

        }

        ,
        handleClose: function (tag, tags) {

            tags.splice(tags.indexOf(tag), 1);
        },
        beforeEnter: function () {
            console.log('beforeEnter');
        }
        ,
        getTaskClassName(row) {
            var r = ""

            this.taskClassArray.forEach(function (element) {
                if (row.taskClassId == element.id) {

                    //  
                    r = element.name;
                    return false;
                }

            }, this);
            return r;
        },




    },


    mounted() {
        this.getTaskList();
    },
    components: {

        taskInfo
    }


} 